import { parse } from 'qs';

async function query(page){
    return new Promise(function(resolve,reject){
      setTimeout(function(){
        resolve([{
            id:1,
            costTime:'2016-11-25',
            cost:Math.random()*100,
            status:'缴清'
        },{
            id:2,
            costTime:'2016-11-25',
            cost:Math.random()*100,
            status:'缴清'
        },{
            id:3,
            costTime:'2016-11-25',
            cost:Math.random()*100,
            status:'缴清'
        },{
            id:4,
            costTime:'2016-11-25',
            cost:Math.random()*100,
            status:'缴清'
        },{
            id:5,
            costTime:'2016-11-25',
            cost:Math.random()*100,
            status:'缴清'
        },{
            id:6,
            costTime:'2016-11-25',
            cost:Math.random()*100,
            status:'缴清'
        },{
            id:7,
            costTime:'2016-11-25',
            cost:Math.random()*100,
            status:'缴清'
        }])
      },2000);
    })
}

export default {

  namespace: 'payDetail',

  state: {
      dataSource:[],
      loading:true,
      total:0,
      current:1,
      loading:false
  },

  subscriptions: {
    setup({ dispatch, history }) {
      dispatch({
        type: 'query',
        payload: 1,
      });
    },
  },

  effects: {
    *query({ payload }, { call, put }){
      yield put({ type: 'showLoading' });
      const data = yield call(query, parse(payload));
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            dataSource: data,
            total: data.length,
            current: payload.page,
          },
        });
      }
    },
    *fetchRemote({ payload }, { call, put }) {
    },
  },
  reducers: {
    fetch(state, action) {
      return { ...state, ...action.payload };
    },
    showLoading(state) {
      return { ...state, loading: true };
    },
    showModal(state, action){
        return { ...state, ...action.payload, modalVisible: true };
    },
    querySuccess(state,action){
        return { ...state, ...action.payload, loading: false };
    },
    hideModal(state){
        return { ...state, modalVisible: false };
    }
  },

}
